﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Huirui.Cavan.Core.MyLinq;
using Huirui.Cavan.Core.Persistent;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Core.Extension;
using Huirui.Cavan.Core.Helper;

namespace Huirui.Cavan.Domain.MainBoundedContext.PurchaseModule
{
    public class PurchaseOrderSpecification : ISpecification<PurchaseOrder, PurchaseOrderSearch>
    {
        #region ISpecification<PurchaseOrder,PurchaseOrderSearch> 成员

        public PurchaseOrderSearch SearchParams
        {
            get;
            set;
        }

        #endregion

        #region ISpecification<PurchaseOrder> 成员

        public System.Linq.Expressions.Expression<Func<PurchaseOrder, bool>> Expression
        {
            get
            {
                IQueryBuilder<PurchaseOrder> queryBuilder = QueryBuilder.Create<PurchaseOrder>();

                if (SearchParams.IsNotNull())
                {
                    if (SearchParams.ID.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(po => po.Id, SearchParams.ID);
                    }
                    if (SearchParams.StartDate.HasValue && SearchParams.EndDate.HasValue)
                    {
                        queryBuilder = queryBuilder.Greater(po => po.CreateDate, SearchParams.StartDate.Value);
                        queryBuilder = queryBuilder.Less(po => po.CreateDate, SearchParams.EndDate.Value);
                    }
                    if (SearchParams.SupplierCode.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(po => po.Supplier.Id, SearchParams.SupplierCode);
                    }
                    if (SearchParams.PurchaseEmployeeCode.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(po => po.PurchaseEmployee.Id, SearchParams.PurchaseEmployeeCode);
                    }
                    if (SearchParams.WareHouseCode.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(po => po.WareHouse.Id, SearchParams.WareHouseCode);
                    }
                    if (SearchParams.StockInStatus.HasValue)
                    {
                        queryBuilder = queryBuilder.Equals(po => (int)po.StockInStatus, SearchParams.StockInStatus.Value);
                    }
                    if (SearchParams.StoskInStatues.IsNotEmpty())
                    {
                        string[] temp = StringHelper.ClearLastChar(SearchParams.StoskInStatues).Split(',');
                        List<int> stockInStatus = new List<int>();
                        foreach (string s in temp)
                        {
                            stockInStatus.Add(int.Parse(s));
                        }
                        queryBuilder = queryBuilder.In(po => (int)po.StockInStatus, stockInStatus.ToArray());
                    }
                    if (SearchParams.CheckedStatus.HasValue)
                    {
                        queryBuilder = queryBuilder.Equals(po => (int)po.CheckedStatus, SearchParams.CheckedStatus.Value);
                    }
                }

                return queryBuilder.Expression.Expand();
            }
        }

        #endregion
    }
}
